const baseURL = 'https://mall.alipayjf.com';
/**
 * GET请求封装
 */
function get(url, data = {}) {
    return request(url, data, 'GET')
}

/**
 * POST请求封装
 */
function post(url, data = {}) {
    return request(url, data, 'POST')
}

/**
 * request
 */
 const accountInfo = xhs.getAccountInfoSync();
function request(url, data = {}, method = "GET") {
    var contentType = 'application/json'
    return new Promise(function(resolve, reject) {
        xhs.request({
            url: baseURL + url,
            data: data,
            method: method,
            header: {
                'Content-Type': contentType,
				'miniAppid': accountInfo.miniProgram.appId,
                'miniopenid': xhs.getStorageSync('token'),
                'Authorization':'Bearer '+ xhs.getStorageSync('sysToken')// 核销
            },
            success: function(res) {
                if (res.statusCode == 200) {
                    //请求正常200
                    if (res.data.code == 200) {
                        xhs.hideLoading()
                        //正常
                        resolve(res.data);
                    } else if(res.data.code == 902){
                        xhs.navigateTo({
                            url: "/entry/login/index"
                        });
                        resolve(res.data);
                    } else {
                        xhs.hideLoading()
                        //错误
                        xhs.showModal({
                            title: '提示',
                            content: res.data.msg,
                            showCancel: false,
                        })
                    }
                } else {
                    console.log('----',res)
                    //请求失败
                    reject("请求失败：" + res.statusCode)
                }
            },
            fail: function(err) {
                console.log('-err---',err)
                //服务器连接异常
                reject("服务器连接异常，请检查网络再试")
            }
        })
    });
}

module.exports = {
    baseURL,
    request,
    get,
    post
}
